package com.xca.znb.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.xca.znb.R;
import com.xca.znb.entity.NewsLetter;
import com.xca.znb.entity.User;
import com.xca.znb.model.impl.MessageManagerImpl;
import com.xca.znb.presenter.MessageManagerPresenter;

import java.util.List;

public class MessageFragment extends Fragment {
    private View message_fragment ;

    private LinearLayout feedback ;
    private ListView mgeList ;
    private List<NewsLetter> nList ;
    private ArrayAdapter<NewsLetter> adapter ;
    private MessageManagerPresenter presenter ;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (message_fragment == null){
            message_fragment = inflater.inflate(R.layout.my_message , container , false);
        }
        feedback = message_fragment.findViewById(R.id.enter_my_feedback);
        mgeList = message_fragment.findViewById(R.id.my_messages_list);


        MessageManagerImpl messageManager = new MessageManagerImpl(getActivity());
        presenter = new MessageManagerPresenter(messageManager);

        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");

        nList = presenter.getNewsletterByPhone(phone);

        adapter = new ArrayAdapter<NewsLetter>(getActivity() , R.layout.newsletter_item , R.id.newsletter_show_name , nList){
            public View getView(int position , View converView , ViewGroup parent){
                View view = super.getView(position, converView, parent);
                ImageView showImg = view.findViewById(R.id.newsletter_show_img);
                TextView showName = view.findViewById(R.id.newsletter_show_name);
                TextView showId = view.findViewById(R.id.newsletter_show_phone);
                TextView showTime = view.findViewById(R.id.newsletter_show_time);
                NewsLetter newsLetter = nList.get(position);
                if (newsLetter.getPhone1().equals(phone)){
                    User user = presenter.getUserByPhone(newsLetter.getPhone2());
                    if (user != null){
                        if (user.getName() == null){
                            showName.setText(newsLetter.getPhone2());
                        }else {
                            showName.setText(user.getName());
                        }
                        Glide.with(getActivity())
                                .load(user.getConcern())
                                .into(showImg);
                    }
                    showId.setText(newsLetter.getPhone2());
                } else if (newsLetter.getPhone2().equals(phone)) {
                    User user = presenter.getUserByPhone(newsLetter.getPhone1());
                    if (user != null){
                        if (user.getName() == null){
                            showName.setText(newsLetter.getPhone1());
                        }else {
                            showName.setText(user.getName());
                        }
                        Glide.with(getActivity())
                                .load(user.getConcern())
                                .into(showImg);
                    }
                    showId.setText(newsLetter.getPhone1());
                }
                showTime.setText(newsLetter.getTime());

                return view ;
            }
        };

        mgeList.setAdapter(adapter);

        mgeList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView showId = view.findViewById(R.id.newsletter_show_phone);
                String receiveId = showId.getText().toString() ;
                Intent intent = new Intent(getActivity() , MessageActivity.class);
                intent.putExtra("goods_receive" , receiveId) ;
                startActivity(intent);
            }
        });

        feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity() , MyFeedback.class);
                startActivity(intent);
            }
        });
        return message_fragment ;
    }

    @Override
    public void onResume() {
        super.onResume();
        // 刷新数据
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String phone = sharedPreferences.getString("phone" , "");

        nList = presenter.getNewsletterByPhone(phone);

        adapter = new ArrayAdapter<NewsLetter>(getActivity() , R.layout.newsletter_item , R.id.newsletter_show_name , nList){
            public View getView(int position , View converView , ViewGroup parent){
                View view = super.getView(position, converView, parent);
                ImageView showImg = view.findViewById(R.id.newsletter_show_img);
                TextView showName = view.findViewById(R.id.newsletter_show_name);
                TextView showId = view.findViewById(R.id.newsletter_show_phone);
                TextView showTime = view.findViewById(R.id.newsletter_show_time);
                NewsLetter newsLetter = nList.get(position);
                if (newsLetter.getPhone1().equals(phone)){
                    User user = presenter.getUserByPhone(newsLetter.getPhone2());
                    if (user != null){
                        if (user.getName() == null){
                            showName.setText(newsLetter.getPhone2());
                        }else {
                            showName.setText(user.getName());
                        }
                        Glide.with(getActivity())
                                .load(user.getConcern())
                                .into(showImg);
                    }
                    showId.setText(newsLetter.getPhone2());
                } else if (newsLetter.getPhone2().equals(phone)) {
                    User user = presenter.getUserByPhone(newsLetter.getPhone1());
                    if (user != null){
                        if (user.getName() == null){
                            showName.setText(newsLetter.getPhone1());
                        }else {
                            showName.setText(user.getName());
                        }
                        Glide.with(getActivity())
                                .load(user.getConcern())
                                .into(showImg);
                    }
                    showId.setText(newsLetter.getPhone1());
                }
                showTime.setText(newsLetter.getTime());

                return view ;
            }
        };

        mgeList.setAdapter(adapter);
    }

}
